** Descriptive analysis of EI across 4 waves
** Uses data generated from eisurveys_dataorg.do
clear
set scheme s1color 

** Monthly data
clear	
use monthly_energy_insecurity.dta
svyset caseid_w1 [pweight=weight]

** Figure 1
recode hhbill notice disconnect(1=100)

** EI measures over months
svy: reg hhbill i.month
margins month
*marginsplot, recastci(rarea) ciopt(color(%25)) title("Bill") ytitle("Proportion of Respondents" " ") xtitle("Month") saving(tmp1, replace)
marginsplot, recast(scatter) title("      Unable to pay bill") ytitle("Percentage of respondents" " ") xlabel(,labsize(vsmall)) plot1opts(msymbol(O) mcolor(black)) ci1opts(lcolor(black)) xtitle("Month")  saving(tmp1, replace)

svy: reg notice i.month
margins month
*marginsplot, recastci(rarea) ciopt(color(%25)) title("Notice") ytitle(" ") xtitle("Month") saving(tmp2, replace)
marginsplot, recast(scatter) title("      Received shut-off notice") ytitle(" ") xlabel(,labsize(vsmall)) plot1opts(msymbol(O) mcolor(black)) ci1opts(lcolor(black)) xtitle("Month") saving(tmp2, replace)

svy: reg disconnect i.month
margins month
marginsplot, recast(scatter)  title("      Disconnnected from service") ytitle(" ") xlabel(,labsize(vsmall)) plot1opts(msymbol(O) mcolor(black)) ci1opts(lcolor(black)) xtitle("Month") saving(tmp3, replace)

graph combine tmp1.gph tmp2.gph tmp3.gph, ycommon xcommon row(1) saving(figure_1, replace)

forvalues i = 1(1)3 {
	erase tmp`i'.gph
	} 

** Figure S2
** Graph EI measures over months across race
** Bill 
svy: reg hhbill i.month if white==1
margins month
marginsplot, recast(scatter) title("Bill, Whites") ytitle("Percentage of respondents")  plot1opts(msymbol(O) mcolor(black)) ci1opts(lcolor(black)) xtitle("Month") xlabel(,labsize(vsmall)) ylabel(,labsize(vsmall)) saving(tmp1, replace)

svy: reg hhbill i.month if black==1
margins month
marginsplot, recast(scatter) title("Bill, Blacks") ytitle(" ")  plot1opts(msymbol(O) mcolor(black)) ci1opts(lcolor(black)) xtitle("Month") xlabel(,labsize(vsmall)) ylabel(,labsize(vsmall)) saving(tmp2, replace)

svy: reg hhbill i.month if hispanic==1
margins month
marginsplot, recast(scatter) title("Bill, Hispanics") ytitle(" ")  plot1opts(msymbol(O) mcolor(black)) ci1opts(lcolor(black)) xtitle("Month") xlabel(,labsize(vsmall)) ylabel(,labsize(vsmall)) saving(tmp3, replace)

** Notice 
svy: reg notice i.month if white==1
margins month
marginsplot, recast(scatter) title("Notice, Whites") ytitle("Percentage of respondents")  plot1opts(msymbol(O) mcolor(black)) ci1opts(lcolor(black))  xtitle("Month") xlabel(,labsize(vsmall)) ylabel(,labsize(vsmall)) saving(tmp4, replace)

svy: reg notice i.month if black==1
margins month
marginsplot, recast(scatter) title("Notice, Blacks") ytitle(" ")  plot1opts(msymbol(O) mcolor(black)) ci1opts(lcolor(black))  xtitle("Month") xlabel(,labsize(vsmall)) ylabel(,labsize(vsmall)) saving(tmp5, replace)

svy: reg notice i.month if hispanic==1
margins month
marginsplot, recast(scatter) title("Notice, Hispanics") ytitle(" ")  plot1opts(msymbol(O) mcolor(black)) ci1opts(lcolor(black))  xtitle("Month") xlabel(,labsize(vsmall)) ylabel(,labsize(vsmall)) saving(tmp6, replace)

** Disconnect 
svy: reg disconnect i.month if white==1
margins month
marginsplot, recast(scatter) title("Disconnect, Whites") ytitle("Percentage of respondents")  plot1opts(msymbol(O) mcolor(black)) ci1opts(lcolor(black))  xtitle("Month") xlabel(,labsize(vsmall)) ylabel(,labsize(vsmall)) saving(tmp7, replace)

svy: reg disconnect i.month if black==1
margins month
marginsplot, recast(scatter) title("Disconnect, Blacks") ytitle(" ")  plot1opts(msymbol(O) mcolor(black)) ci1opts(lcolor(black))  xtitle("Month") xlabel(,labsize(vsmall)) ylabel(,labsize(vsmall)) saving(tmp8, replace)

svy: reg disconnect i.month if hispanic==1
margins month
marginsplot, recast(scatter) title("Disconnect, Hispanics") ytitle(" ")  plot1opts(msymbol(O) mcolor(black)) ci1opts(lcolor(black))  xtitle("Month") xlabel(,labsize(vsmall)) ylabel(,labsize(vsmall)) saving(tmp9, replace)

graph combine tmp1.gph tmp2.gph tmp3.gph tmp4.gph tmp5.gph tmp6.gph tmp7.gph tmp8.gph tmp9.gph, ycommon xcommon  saving(figure_s2, replace)

forvalues i = 1(1)9 {
	erase tmp`i'.gph
	} 

** Figure S3	
** Graph EI measures over months across income
svy: reg hhbill i.month if under_100_fpl==1
margins month
marginsplot, recast(scatter) title("   Bill, Under 100% FPL") ytitle("Percentage of respondents")  plot1opts(msymbol(O) mcolor(black)) ci1opts(lcolor(black))  xtitle("Month") xlabel(,labsize(vsmall)) ylabel(,labsize(vsmall)) saving(tmp1, replace)

svy: reg hhbill i.month if FPL_100_150==1
margins month
marginsplot, recast(scatter) title("   Bill, 100-150% FPL") ytitle(" ")  plot1opts(msymbol(O) mcolor(black)) ci1opts(lcolor(black))  xtitle("Month") xlabel(,labsize(vsmall)) ylabel(,labsize(vsmall)) saving(tmp2, replace)

svy: reg hhbill i.month if FPL_150_200==1
margins month
marginsplot, recast(scatter) title("   Bill, 150-200% FPL") ytitle(" ")  plot1opts(msymbol(O) mcolor(black)) ci1opts(lcolor(black))  xtitle("Month") xlabel(,labsize(vsmall)) ylabel(,labsize(vsmall)) saving(tmp3, replace)

svy: reg notice i.month if under_100_fpl==1
margins month
marginsplot, recast(scatter) title("   Notice, Under 100% FPL") ytitle("Percentage of respondents")  plot1opts(msymbol(O) mcolor(black)) ci1opts(lcolor(black)) xtitle("Month") xlabel(,labsize(vsmall)) ylabel(,labsize(vsmall)) saving(tmp4, replace)

svy: reg notice i.month if FPL_100_150==1
margins month
marginsplot, recast(scatter) title("   Notice, 100-150% FPL") ytitle(" ") plot1opts(msymbol(O) mcolor(black)) ci1opts(lcolor(black)) xtitle("Month") xlabel(,labsize(vsmall)) ylabel(,labsize(vsmall)) saving(tmp5, replace)

svy: reg notice i.month if FPL_150_200==1
margins month
marginsplot, recast(scatter) title("   Notice, 150-200% FPL") ytitle(" ") plot1opts(msymbol(O) mcolor(black)) ci1opts(lcolor(black)) xtitle("Month") xlabel(,labsize(vsmall)) ylabel(,labsize(vsmall)) saving(tmp6, replace)

svy: reg disconnect i.month if under_100_fpl==1
margins month
marginsplot, recast(scatter) title("   Disconnect, Under 100% FPL") ytitle("Percentage of respondents") plot1opts(msymbol(O) mcolor(black)) ci1opts(lcolor(black)) xtitle("Month") xlabel(,labsize(vsmall)) ylabel(,labsize(vsmall)) saving(tmp7, replace)

svy: reg disconnect i.month if FPL_100_150==1
margins month
marginsplot, recast(scatter) title("   Disconnect, 100-150% FPL") ytitle(" ") plot1opts(msymbol(O) mcolor(black)) ci1opts(lcolor(black)) xtitle("Month") xlabel(,labsize(vsmall)) ylabel(,labsize(vsmall)) saving(tmp8, replace)

svy: reg disconnect i.month if FPL_150_200==1
margins month
marginsplot, recast(scatter) title("   Disconnect, 150-200% FPL") ytitle(" ") plot1opts(msymbol(O) mcolor(black)) ci1opts(lcolor(black)) xtitle("Month") xlabel(,labsize(vsmall)) ylabel(,labsize(vsmall)) saving(tmp9, replace)

graph combine tmp1.gph tmp2.gph tmp3.gph tmp4.gph tmp5.gph tmp6.gph tmp7.gph tmp8.gph tmp9.gph, ycommon xcommon  saving(figure_s3, replace)

forvalues i = 1(1)9 {
	erase tmp`i'.gph
	} 

** Figure S4
** Graph EI measures over months across respondents with and w/out kids age 5 and younger
gen kids=1 if hh_under5>=1 & hh_under5!=.
replace kids=0 if hh_under5==0 & hh_under5!=.

svy: reg hhbill i.month if kids==1
margins month
marginsplot, recast(scatter) title("      Bill, Children") ytitle("Percentage of respondents" " ") plot1opts(msymbol(O) mcolor(black)) ci1opts(lcolor(black))  xtitle("Month") xlabel(,labsize(vsmall)) ylabel(,labsize(vsmall)) saving(tmp1, replace)

svy: reg hhbill i.month if kids==0
margins month
marginsplot, recast(scatter) title("      Bill, No children ") ytitle(" ") plot1opts(msymbol(O) mcolor(black)) ci1opts(lcolor(black))  xtitle("Month") xlabel(,labsize(vsmall)) ylabel(,labsize(vsmall)) saving(tmp2, replace)

svy: reg notice i.month if kids==1
margins month
marginsplot, recast(scatter) title("      Notice, Children") ytitle("Percentage of respondents" " ") plot1opts(msymbol(O) mcolor(black)) ci1opts(lcolor(black))  xtitle("Month") xlabel(,labsize(vsmall)) ylabel(,labsize(vsmall)) saving(tmp3, replace)

svy: reg notice i.month if kids==0
margins month
marginsplot, recast(scatter) title("      Notice, No children") ytitle(" ") plot1opts(msymbol(O) mcolor(black)) ci1opts(lcolor(black))  xtitle("Month") xlabel(,labsize(vsmall)) ylabel(,labsize(vsmall)) saving(tmp4, replace)

svy: reg disconnect i.month if kids==1
margins month
marginsplot, recast(scatter) title("      Disconnect, Children") ytitle("Percentage of respondents" " ") plot1opts(msymbol(O) mcolor(black)) ci1opts(lcolor(black))  xtitle("Month") xlabel(,labsize(vsmall)) ylabel(,labsize(vsmall)) saving(tmp5, replace)

svy: reg disconnect i.month if kids==0
margins month
marginsplot, recast(scatter) title("      Disconnect, No children") ytitle(" ") plot1opts(msymbol(O) mcolor(black)) ci1opts(lcolor(black))  xtitle("Month") xlabel(,labsize(vsmall)) ylabel(,labsize(vsmall)) saving(tmp6, replace)

graph combine tmp1.gph tmp2.gph tmp3.gph tmp4.gph tmp5.gph tmp6.gph, ycommon xcommon row(3) saving(figure_s4, replace)

forvalues i = 1(1)6 {
	erase tmp`i'.gph
	} 

** Figure S5	
** Graph EI measures over months across respondents with and w/out one poor housing condition
gen dwcond=1 if dwelling_condition>=1 & dwelling_condition!=.
replace dwcond=0 if dwelling_condition==0 & dwelling_condition!=.

svy: reg hhbill i.month if dwcond==1
margins month
marginsplot, recast(scatter) title("      Bill, Poor housing condition") ytitle("Percentage of Respondents" " ") plot1opts(msymbol(O) mcolor(black)) ci1opts(lcolor(black))  xtitle("Month") xlabel(,labsize(vsmall)) ylabel(,labsize(vsmall)) saving(tmp1, replace)

svy: reg hhbill i.month if dwcond==0
margins month
marginsplot, recast(scatter) title("      Bill, No poor housing condition ") ytitle(" ") plot1opts(msymbol(O) mcolor(black)) ci1opts(lcolor(black))  xtitle("Month") xlabel(,labsize(vsmall)) ylabel(,labsize(vsmall)) saving(tmp2, replace)

svy: reg notice i.month if dwcond==1
margins month
marginsplot, recast(scatter) title("      Notice, Poor housing condition") ytitle("Percentage of Respondents" " ") plot1opts(msymbol(O) mcolor(black)) ci1opts(lcolor(black))  xtitle("Month") xlabel(,labsize(vsmall)) ylabel(,labsize(vsmall)) saving(tmp3, replace)

svy: reg notice i.month if dwcond==0
margins month
marginsplot, recast(scatter) title("      Notice, No poor housing condition") ytitle(" ") plot1opts(msymbol(O) mcolor(black)) ci1opts(lcolor(black))  xtitle("Month") xlabel(,labsize(vsmall)) ylabel(,labsize(vsmall)) saving(tmp4, replace)

svy: reg disconnect i.month if dwcond==1
margins month
marginsplot, recast(scatter) title("      Disconnect, Poor housing condition") ytitle("Percentage of Respondents" " ") plot1opts(msymbol(O) mcolor(black)) ci1opts(lcolor(black))  xtitle("Month") xlabel(,labsize(vsmall)) ylabel(,labsize(vsmall)) saving(tmp5, replace)

svy: reg disconnect i.month if dwcond==0
margins month
marginsplot, recast(scatter) title("      Disconnect, No poor housing condition") ytitle(" ") plot1opts(msymbol(O) mcolor(black)) ci1opts(lcolor(black))  xtitle("Month") xlabel(,labsize(vsmall)) ylabel(,labsize(vsmall)) saving(tmp6, replace)

graph combine tmp1.gph tmp2.gph tmp3.gph tmp4.gph tmp5.gph tmp6.gph, ycommon xcommon row(3) saving(figure_s5, replace)

forvalues i = 1(1)6 {
	erase tmp`i'.gph
	} 
	
** Create measures of acute v chronic EI (acute=short-term; chronic=persistent in ERL paper)
** In graphs, just use month 13, so as not to inflate
sort caseid_w1
recode hhbill notice disconnect(100=1)
by caseid_w1: egen hhbill_number=sum(hhbill)
by caseid_w1: egen notice_number=sum(notice)
by caseid_w1: egen disconnect_number=sum(disconnect)

** Acute = any one month
** Chronic = more than one month 
gen hhbill_acute=1 if hhbill_number==1 
replace hhbill_acute=0 if hhbill_number>1
gen hhbill_chronic=1 if hhbill_number>1
replace hhbill_chronic=0 if hhbill_number==1

gen notice_acute=1 if notice_number==1
replace notice_acute=0 if notice_number>1
gen notice_chronic=1 if notice_number>1
replace notice_chronic=0 if notice_number==1

gen disconnect_acute=1 if disconnect_number==1
replace disconnect_acute=0 if disconnect_number>1

gen disconnect_chronic=1 if disconnect_number>1
replace disconnect_chronic=0 if disconnect_number==1

recode hhbill_acute hhbill_chronic notice_acute notice_chronic disconnect_acute disconnect_chronic (1=100)

** Figure 2
svy: mean hhbill_acute hhbill_chronic if month==13
svy: mean notice_acute notice_chronic if month==13
svy: mean disconnect_acute disconnect_chronic if month==13
save foo, replace
clear
** Create new dataset (above summary statistics entered into dataset outside this do file and then brought back in to make graph; dataset named: rcap_data)
use rcap_data, clear

label define acute_chronic 1 "Short-term" 2 "Persistent"
label values bill notice disconnect acute_chronic

graph twoway (bar m_bill bill if bill==1, barwidth(0.9) title("Unable to pay bill", size(medium)) ytitle("Percentage of respondents among those energy insecure" " ") bcolor(black) xtitle(" ") ylabel(0(10)100)     xlabel( 1 "Short-term" 2 "Persistent", noticks) legend(off) xlabel(1(1)2)) (bar m_bill bill if bill==2, bcolor(gs7) xlabel( 1 "Short-term" 2 "Persistent", noticks)) (rcap hi_bill lo_bill bill, saving(tmp1, replace))

graph twoway (bar m_notice notice if notice==1, barwidth(0.9) title("Received shut-off notice", size(medium)) ytitle("Percentage of respondents among those energy insecure" " ") bcolor(black) xtitle(" ") ylabel(0(10)100)     xlabel( 1 "Short-term" 2 "Persistent", noticks) legend(off) xlabel(1(1)2)) (bar m_notice notice if notice==2, bcolor(gs7) xlabel( 1 "Short-term" 2 "Persistent", noticks)) (rcap hi_notice lo_notice notice, saving(tmp2, replace))

graph twoway (bar m_disconnect disconnect if disconnect==1, barwidth(0.9) title("Disconnected from service", size(medium)) ytitle("Percentage of respondents among those energy insecure" " ") bcolor(black) xtitle(" ") ylabel(0(10)100) xlabel( 1 "Short-term" 2 "Persistent", noticks) legend(off) xlabel(1(1)2)) (bar m_disconnect disconnect if disconnect==2, bcolor(gs7) xlabel( 1 "Short-term" 2 "Persistent", noticks)) (rcap hi_disconnect lo_disconnect disconnect, saving(tmp3, replace))
	
graph combine tmp1.gph tmp2.gph tmp3.gph, ycommon xcommon row(1) saving(figure_2, replace)

forvalues i = 1(1)3 {
	erase tmp`i'.gph
	}

clear
use foo

** Create additional measures of persistent EI for analysis
recode hhbill_acute hhbill_chronic notice_acute notice_chronic disconnect_acute disconnect_chronic (100=1)

** Bill pay
sort caseid_w1 month
gen foo=1
egen hhbill_count=sum(foo) if hhbill==1, by(caseid_w1)
replace hhbill_count=0 if hhbill_count==.

** Any one month
gen hhbill_1month=1 if hhbill_count==1
replace hhbill_1month=0 if hhbill_1month==.

** Two successive months
sort caseid_w1 month
gen hhbill_2months=1 if hhbill==1 & hhbill[_n-1]==1 & caseid_w1==caseid_w1[_n-1]
replace hhbill_2months=0 if hhbill_2months==.

** Three successive months
sort caseid_w1 month
gen hhbill_3months=1 if hhbill==1 & hhbill[_n-1]==1 & hhbill[_n-2]==1 & caseid_w1==caseid_w1[_n-2]
replace hhbill_3months=0 if hhbill_3months==.

** Four successive months
sort caseid_w1 month
gen hhbill_4months=1 if hhbill==1 & hhbill[_n-1]==1 & hhbill[_n-2]==1 & hhbill[_n-3]==1 & caseid_w1==caseid_w1[_n-3]
replace hhbill_4months=0 if hhbill_4months==.

** Five successive months
sort caseid_w1 month
gen hhbill_5months=1 if hhbill==1 & hhbill[_n-1]==1 & hhbill[_n-2]==1 & hhbill[_n-3]==1 & hhbill[_n-4]==1 &  caseid_w1==caseid_w1[_n-4]
replace hhbill_5months=0 if hhbill_5months==.

** Six successive months
sort caseid_w1 month
gen hhbill_6months=1 if hhbill==1 & hhbill[_n-1]==1 & hhbill[_n-2]==1 & hhbill[_n-3]==1 & hhbill[_n-4]==1 & hhbill[_n-5]==1 & caseid_w1==caseid_w1[_n-5]
replace hhbill_6months=0 if hhbill_6months==.


** Notice
sort caseid_w1 month
egen notice_count=sum(foo) if notice==1, by(caseid_w1)
replace notice_count=0 if notice_count==.

** Any one month
gen notice_1month=1 if notice_count==1
replace notice_1month=0 if notice_1month==.

** Two successive months
sort caseid_w1 wave
gen notice_2months=1 if notice==1 & notice[_n-1]==1 & caseid_w1==caseid_w1[_n-1]
replace notice_2months=0 if notice_2months==.

** Three successive months
sort caseid_w1 month
gen notice_3months=1 if notice==1 & notice[_n-1]==1 & notice[_n-2]==1 & caseid_w1==caseid_w1[_n-2]
replace notice_3months=0 if notice_3months==.

** Four successive months
sort caseid_w1 month
gen notice_4months=1 if notice==1 & notice[_n-1]==1 & notice[_n-2]==1 & notice[_n-3]==1 & caseid_w1==caseid_w1[_n-3]
replace notice_4months=0 if notice_4months==.

** Five successive months
sort caseid_w1 month
gen notice_5months=1 if notice==1 & notice[_n-1]==1 & notice[_n-2]==1 & notice[_n-3]==1 & notice[_n-4]==1 &  caseid_w1==caseid_w1[_n-4]
replace notice_5months=0 if notice_5months==.

** Six successive months
sort caseid_w1 month
gen notice_6months=1 if notice==1 & notice[_n-1]==1 & notice[_n-2]==1 & notice[_n-3]==1 & notice[_n-4]==1 & notice[_n-5]==1 & caseid_w1==caseid_w1[_n-5]
replace notice_6months=0 if notice_6months==.

** Disconnect
sort caseid_w1 month
egen disconnect_count=sum(foo) if disconnect==1, by(caseid_w1)
replace disconnect_count=0 if disconnect_count==.

** Any one month
gen disconnect_1month=1 if disconnect_count==1
replace disconnect_1month=0 if disconnect_1month==.

** Two successive months
sort caseid_w1 month
gen disconnect_2months=1 if disconnect==1 & disconnect[_n-1]==1 & caseid_w1==caseid_w1[_n-1]
replace disconnect_2months=0 if disconnect_2months==.

** Three successive waves
sort caseid_w1 month
gen disconnect_3months=1 if disconnect==1 & disconnect[_n-1]==1 & disconnect[_n-2]==1 & caseid_w1==caseid_w1[_n-2]
replace disconnect_3months=0 if disconnect_3months==.

** Four successive waves
sort caseid_w1 month
gen disconnect_4months=1 if disconnect==1 & disconnect[_n-1]==1 & disconnect[_n-2]==1 & disconnect[_n-3]==1 & caseid_w1==caseid_w1[_n-3]
replace disconnect_4months=0 if disconnect_4months==.

** Five successive months
sort caseid_w1 month
gen disconnect_5months=1 if disconnect==1 & disconnect[_n-1]==1 & disconnect[_n-2]==1 & disconnect[_n-3]==1 & disconnect[_n-4]==1 & caseid_w1==caseid_w1[_n-4]
replace disconnect_5months=0 if disconnect_5months==.

** Six successive months
sort caseid_w1 month
gen disconnect_6months=1 if disconnect==1 & disconnect[_n-1]==1 & disconnect[_n-2]==1 & disconnect[_n-3]==1 & disconnect[_n-4]==1 & disconnect[_n-5]==1 & caseid_w1==caseid_w1[_n-5]
replace disconnect_6months=0 if disconnect_6months==.

** Save dataset for regression analysis
save thirteenmonths_analysis, replace


* Figure S1
** Wave data
use waves_energy_insecurity.dta, clear
svyset caseid_w1 [pweight=weight]

** EI measures over waves
recode hhbill notice disconnect(1=100)

svy: reg hhbill i.wave
margins wave
marginsplot, recast(scatter) title("      Unable to Pay Bill") ytitle("Percentage of Respondents" " ") xlabel(,labsize(vsmall)) plot1opts(msymbol(O) mcolor(black)) ci1opts(lcolor(black)) xtitle("Wave") saving(tmp1, replace)

svy: reg notice i.wave
margins wave
marginsplot, recast(scatter) title("      Received Shut-off Notice") ytitle(" ") xlabel(,labsize(vsmall)) plot1opts(msymbol(O) mcolor(black)) ci1opts(lcolor(black)) xtitle("Wave") saving(tmp2, replace)

svy: reg disconnect i.wave
margins wave
marginsplot, recast(scatter) title("      Disconnnected from Service") ytitle(" ") xlabel(,labsize(vsmall)) plot1opts(msymbol(O) mcolor(black)) ci1opts(lcolor(black)) xtitle("Wave") saving(tmp3, replace)


graph combine tmp1.gph tmp2.gph tmp3.gph, ycommon xcommon row(1) saving(figure_s1, replace)

forvalues i = 1(1)3 {
	erase tmp`i'.gph
	} 

